package com.demo.impl.controller;

import com.demo.api.user.domain.TestLimit;
import com.demo.api.user.domain.TestMultiLimitDto;
import com.demo.api.user.service.InsertLimitService;
import com.demo.api.user.service.QueryLimitService;
import com.demo.api.user.service.TestMultiLimitService;
import com.demo.common.annotation.Clog;
import com.demo.impl.service.OpenNewUserMDMTService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 *
 * @author
 * @since
 */
@RestController
@Slf4j
public class Test002 {
  @Reference(version = "1.0.0", check = false)
  QueryLimitService queryLimitService;

  @Reference(version = "1.0.0", check = false)
  InsertLimitService insertLimitService;

  @Reference(version = "1.0.0", check = false)
  TestMultiLimitService testMultiLimitService;

  @Autowired
  OpenNewUserMDMTService openNewUserMDMTService;


  @GetMapping("/Mdmt-read-write-divide-query")
  @Clog(descrption = "读写分离-查询测试")
  public String queryLimitForRWD(){
    String ret = null;
    try {
      ret = queryLimitService.queryById(15).toString();
    } catch (Exception e){
      log.error("{}",e);
      return "Exception";
    }
    return ret;
  }

  @GetMapping("/Mdmt-read-write-divide-write")
  @Clog(descrption = "读写分离-写入测试")
  public String addLimitForRWD(){
    String ret = null;
    try {
      TestLimit testLimit = new TestLimit();
      testLimit.setDt(new Date());
      testLimit.setLimitAmount(new BigDecimal(Math.random()));
      testLimit.setIdno(String.valueOf(Math.random()));
      ret = insertLimitService.addTestLimit(testLimit).getData().toString();
    } catch (Exception e){
      log.error("{}",e);
      return "Exception";
    }
    return ret;
  }

  @GetMapping("/Multi-Lib-Tab-Insert")
  @Clog(descrption = "多库多表-写入测试")
  public String insertMultiLimit(){
    String ret = null;
    try {
      List<TestMultiLimitDto> testMultiLimitList = new ArrayList<>(64);
      for (int i = 1; i < 5; i++) {
        testMultiLimitList.add(TestMultiLimitDto.builder()
            .libId(1)
            .tabId(i)
            .idno(UUID.randomUUID().toString().replace("-", "").substring(0, 31))
            .dt(new Date())
            .limitAmount(new BigDecimal((new Random()).nextFloat()))
            .build());
      }

      for (int i = 1; i < 5; i++) {
        testMultiLimitList.add(TestMultiLimitDto.builder()
            .libId(2)
            .tabId(i)
            .idno(UUID.randomUUID().toString().replace("-", "").substring(0, 31))
            .dt(new Date())
            .limitAmount(new BigDecimal((new Random()).nextFloat()))
            .build());
      }

      ret = testMultiLimitService.addTestLimit(testMultiLimitList).getData().toString();
    } catch (Exception e){
      log.error("{}",e);
      return "Exception " + e.getMessage();
    }
    return ret;
  }

  @GetMapping("/Multi-Lib-Tab-query/{libId}/{tabId}/{id}")
  @Clog(descrption = "多库多表-写入测试")
  public String queryMultiLimit(@PathVariable("libId") Integer libId,
                                @PathVariable("tabId") Integer tabId,
                                @PathVariable("id") Integer id){
    String ret = null;
    try {

      ret = testMultiLimitService.queryById(id, libId, tabId).getData().toString();
    } catch (Exception e){
      log.error("{}",e);
      return "Exception " + e.getMessage();
    }
    return ret;
  }

  @GetMapping("/Multi-Lib-Tab-SEATA/{idno}")
  @Clog(descrption = "多库多表-分布式案例测试")
  public String addUserAndLimit(@PathVariable("idno") String idno){
    String ret = null;
    try {
      openNewUserMDMTService.addUserAndLimit(idno);
    } catch (Exception e){
      log.error("{}",e);
      return "Exception " + e.getMessage();
    }
    return "okay";
  }

  @GetMapping("/Multi-Lib-Tab-SEATA-rb/{idno}")
  @Clog(descrption = "多库多表-分布式案例回滚测试")
  public String addUserAndLimitRollback(@PathVariable("idno") String idno){
    String ret = null;
    try {
      openNewUserMDMTService.addUserAndLimitRollback(idno);
    } catch (Exception e){
      log.error("{}",e);
      return "Exception " + e.getMessage();
    }
    return "okay";
  }

}
